package com.golden.thumbnail;


import java.util.ArrayList;
import java.util.List;

import com.golden.thumbnail.callable.CompressCallable;
import com.golden.thumbnail.thread.WorkerThread;
import com.golden.thumbnail.vo.TaskVO;

/**
 * Hello world!
 *
 */
public class ThumbnailUtil {
	
	private WorkerThread workerThread = null;
	
	private static ThreadLocal<ThumbnailUtil> local = new ThreadLocal<ThumbnailUtil>();
	
	private ThumbnailUtil() {
		workerThread = new WorkerThread();
		Thread thread = new Thread(workerThread);
		thread.start();
	}
	
	public static ThumbnailUtil createThumbnailUtil() {
		if(local.get() == null ) {
			ThumbnailUtil ThumbnailUtil = new ThumbnailUtil();
			local.set(ThumbnailUtil);
		}
		return local.get();
	}
	
	public void add(TaskVO taskVO) {
		if(taskVO != null) {
			taskVO.setCallabel(new CompressCallable(taskVO));
			workerThread.addTask(taskVO);
		}
	}
	
	public static void main(String[] args) {
		List<String> scaleList = new ArrayList<String>();
		scaleList.add("150x300");
		scaleList.add("300x600");
		scaleList.add("400x800");
		TaskVO taskVO = new TaskVO();
		taskVO.setInputPath("d:/");
		taskVO.setOutPath("e:/");
		taskVO.setFilename("800.jpg");
		taskVO.setScaleList(scaleList);
		ThumbnailUtil thumbnailUtil = createThumbnailUtil();
		thumbnailUtil.add(taskVO);
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TaskVO taskVO2 = new TaskVO();
		taskVO2.setInputPath("d:/");
		taskVO2.setOutPath("e:/");
		taskVO2.setFilename("801.jpg");
		taskVO2.setScaleList(scaleList);
		thumbnailUtil.add(taskVO2);
	}
}
